/*
 * Copyright 2020 tu.cn All right reserved. This software is the
 * confidential and proprietary information of tu.cn ("Confidential
 * Information"). You shall not disclose such Confidential Information and shall
 * use it only in accordance with the terms of the license agreement you entered
 * into with Tu.cn
 */
package com.onedec.project.controller;

import com.onedec.entity.OnedecClient;
import com.onedec.project.client.DesignercClient;
import com.onedec.project.client.EmpClient;
import com.onedec.project.client.OnedecmaterialsClinet;
import com.onedec.project.entity.OnedecProject;
import com.onedec.project.service.ProjectService;
import com.onedec.project.util.Result;
import com.onedec.project.util.util;
import com.onedec.utils.PageBean;
import com.onedec.utils.ResponseEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 31014
 * @date 2020/7/16 22:54
 */
@RestController
@RequestMapping("project")
public class ProjectController {
    @Resource
    private ProjectService projectService;


    @PostMapping
    public ResponseEntity insertProject(OnedecClient onedecClient) {
        System.out.println(onedecClient);
        final OnedecProject project = new OnedecProject();
        project.setProjectAddress(onedecClient.getClientProvince() + onedecClient.getClientCity() + onedecClient.getClientArea() + onedecClient.getClientAddr());
        project.setProjectCustomerPhone(onedecClient.getClientTel());
        project.setProjectCustomerName(onedecClient.getClientName());
        project.setProjectArea(onedecClient.getClientHousearea());
        project.setProjectInPhase(1);
        project.setProjectSts(1);
        project.setProjectOrderTime(new Date());
        project.setProjectNull(util.getOrderNo());
        projectService.insertProject(project);
        return ResponseEntity.SUCCESS;
    }

    @GetMapping("list")
    public ResponseEntity selectProjectList(Integer page, Integer limit, OnedecProject onedecProject) {
        final HashMap<String, Object> parm = new HashMap<>();
        System.out.println(onedecProject);
        parm.put("page", page);
        parm.put("limit", limit);
        parm.put("address", onedecProject.getProjectAddress());
        parm.put("status", onedecProject.getProjectSts());
        parm.put("starttime", onedecProject.getProjectOrderTime());
        parm.put("endtime", onedecProject.getProjectEndTime());
        parm.put("phase", onedecProject.getProjectInPhase());
        final PageBean<OnedecProject> pageBean = projectService.selectProjectList(parm);
        return new ResponseEntity(pageBean);
    }

    @PutMapping("setDesigner")
    public ResponseEntity setDesigner(Integer eId, String projectNo) {
        projectService.updateProjectByDesigner(eId, projectNo);
        return ResponseEntity.SUCCESS;
    }

    @PutMapping("addDrawings")
    public ResponseEntity addDrawings(Integer eId, String projectNo) {
        projectService.updateProjectByDrawings(eId, projectNo);
        return ResponseEntity.SUCCESS;
    }

    @PutMapping("auditdrawing")
    public ResponseEntity auditDrawing(Integer eId, String projectNo) {
        projectService.updateAuditDrawing(eId, projectNo);
        return ResponseEntity.SUCCESS;
    }

    @PutMapping("addManager")
    public ResponseEntity addManager(Integer eId, String projectNo) {
        projectService.addManager(eId, projectNo);
        return ResponseEntity.SUCCESS;
    }

    @PutMapping("addAssign")
    public ResponseEntity addAssign(Integer eId, String projectNo) {
        projectService.addAssign(eId, projectNo);
        return ResponseEntity.SUCCESS;
    }

    @PutMapping("updateProject")
    public ResponseEntity updateProject(Integer eId, String projectNo, Integer status) {

        projectService.updateStatus(eId, projectNo, status);
        return ResponseEntity.SUCCESS;
    }

    @Autowired
    private EmpClient empClient;
    @Autowired
    private DesignercClient designercClient;
    @Autowired
    private OnedecmaterialsClinet onedecmaterialsClinet;

    @GetMapping("getProjectDetails")
    public ResponseEntity getProjectDetails(String projectNo) {
        final OnedecProject project = projectService.selectProjectByProjectNo(projectNo);
        final Integer stylistId = project.getProjectStylistId();
        final ResponseEntity designer = empClient.SelectById(stylistId);
        final Result byconstruction = designercClient.selectByconstruction(projectNo);
        final Result bycontract = designercClient.selectBycontract(projectNo);
        final Result bydesigner = designercClient.selectBydesigner(projectNo);
        final Result byplan = designercClient.selectByplan(projectNo);
        final HashMap<String, Object> res = new HashMap<>();
        res.put("designer", designer.getData());
        res.put("plan", byplan.getList());
        res.put("contract", bycontract.getList());
        res.put("construction", byconstruction.getList());
        res.put("floorPlan", bydesigner.getList());
        ResponseEntity Pm = null;
        if (project.getProjectPmId() != null && project.getProjectPmId() != 0) {
            Pm = empClient.SelectById(project.getProjectPmId());
            res.put("manager", Pm.getData());
        } else {
            res.put("manager", null);
        }
        if (project.getProjectSupervisorId() != null && project.getProjectSupervisorId() != 0) {
            res.put("supervision", empClient.SelectById(project.getProjectSupervisorId()).getData());
        }
        final ResponseEntity addMaterialList1 = onedecmaterialsClinet.addMaterialList(projectNo, 1);
        final ResponseEntity addMaterialList2 = onedecmaterialsClinet.addMaterialList(projectNo, 2);
        final ResponseEntity addMaterialList3 = onedecmaterialsClinet.addMaterialList(projectNo, 3);
        final ResponseEntity addMaterialList4 = onedecmaterialsClinet.addMaterialList(projectNo, 4);
        final ResponseEntity addMaterialList5 = onedecmaterialsClinet.addMaterialList(projectNo, 5);
        res.put("inPhase1material", addMaterialList1.getData());
        res.put("inPhase2material", addMaterialList2.getData());
        res.put("inPhase3material", addMaterialList3.getData());
        res.put("inPhase4material", addMaterialList4.getData());
        res.put("inPhase5material", addMaterialList5.getData());
        final ResponseEntity receipt1 = onedecmaterialsClinet.selectReceipt(projectNo, 1);
        final ResponseEntity receipt2 = onedecmaterialsClinet.selectReceipt(projectNo, 2);
        final ResponseEntity receipt3 = onedecmaterialsClinet.selectReceipt(projectNo, 3);
        final ResponseEntity receipt4 = onedecmaterialsClinet.selectReceipt(projectNo, 4);
        final ResponseEntity receipt5 = onedecmaterialsClinet.selectReceipt(projectNo, 5);
        res.put("inPhase1acceptance", receipt1.getData());
        res.put("inPhase2acceptance", receipt2.getData());
        res.put("inPhase3acceptance", receipt3.getData());
        res.put("inPhase4acceptance", receipt4.getData());
        res.put("inPhase5acceptance", receipt5.getData());
        res.put("project",project);
        return new ResponseEntity(res);
    }
}